package org.rollinitiative.d20web.gmportal.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class EncounterManagementApplication implements EntryPoint
{

    private EncounterServiceAsync encounterService;
    
    private DockPanel mainPanel;
    
    private EncounterExecutionView executionView;
    
    private BattleLogView battleLogView;
    
    private CombatantsView combatantsView;


    /**
     * This is the entry point method.
     */
    public void onModuleLoad()
    {
        encounterService = (EncounterServiceAsync)GWT.create(EncounterService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) encounterService;
        String moduleRelativeURL = GWT.getModuleBaseURL()
                + "EncounterManagementService";
        endpoint.setServiceEntryPoint(moduleRelativeURL);
        
        encounterService.createNewEncounter("New Encounter", newEncounterCallback);
//        initView();
    }
    
    AsyncCallback<String> newEncounterCallback = new AsyncCallback<String> () {

        /* (non-Javadoc)
         * @see com.google.gwt.user.client.rpc.AsyncCallback#onFailure(java.lang.Throwable)
         */
        public void onFailure(Throwable caught)
        {
            GWT.log("Call for creation of new encounter failed", caught);
            
        }

        /* (non-Javadoc)
         * @see com.google.gwt.user.client.rpc.AsyncCallback#onSuccess(java.lang.Object)
         */
        public void onSuccess(String result)
        {
            GWT.log("Call for creation of new encounter succeeded", null);
            initView(result);
        }
        
    };
    
    private void initView(String encounterID)
    {
        mainPanel = new DockPanel();

        
        executionView = new EncounterExecutionView();
        mainPanel.add(executionView, DockPanel.CENTER);
        executionView.setEncounterService(encounterService);
        executionView.setEncounterID(encounterID);
        executionView.setWidth("100%");
        
        battleLogView = new BattleLogView();
        mainPanel.add(battleLogView, DockPanel.SOUTH);
        battleLogView.setWidth("100%");
        
        combatantsView = new CombatantsView();
        combatantsView.setEncounterID(encounterID);
        combatantsView.loadDefaultData();
        mainPanel.add(combatantsView, DockPanel.WEST);
        combatantsView.setWidth("100%");
        
        
        mainPanel.setWidth("100%");
        mainPanel.setSpacing(4);
        mainPanel.setCellWidth(executionView, "100%");
        RootPanel.get().add(mainPanel);
        
        // event connections amongst views
        combatantsView.addCombatantListener(executionView);
        
    }
}
